<template>
  <div class="home page-component">
    <!-- 滚动图片 -->
    <el-carousel indicator-position="outside">
      <el-carousel-item v-for="item in 2" :key="item">
        <img src="~assets/images/web-banner1.png" alt="">
      </el-carousel-item>
    </el-carousel>

    <!-- 搜索条 -->
    <div class="search-container">
      <div class="search-wrapper">
        <div class="hospital-search">
          <el-autocomplete
              class="search-input"
              prefix-icon="el-icon-search"
              v-model="hosname"
              :fetch-suggestions="querySearchAsync"
              placeholder="点击输入医院名称"
              @select="handleSelect">
            <span slot="suffix" class="search-btn v-link highlight clickable selected">
              搜索
            </span>
          </el-autocomplete>
        </div>
      </div>
    </div>

    <!-- 主要内容 -->
    <div class="bottom">

      <!-- 左侧显示内容 -->
      <div class="left">
        <!-- 条件选择列表 -->
        <div class="home-filter-wrapper">
          <div class="title"> 医院</div>
          <div>
            <div class="filter-wrapper">
              <span class="label">等级：</span>
              <div class="condition-wrapper">
                <span class="item v-link highlight clickable"
                      v-for="(item, index) in hostypeList" :key="index"
                      @click="hostypeSelect(item.value, index)"
                      :class="hostypeActiveIndex == index ? 'selected' : ''">
                  {{item.name}}
                </span>
              </div>
            </div>
            <div class="filter-wrapper">
              <span class="label">地区：</span>
              <div class="condition-wrapper">
                <span class="item v-link highlight clickable"
                      v-for="(item, index) in districtList" :key="index"
                      @click="districtSelect(item.value, index)"
                      :class="districtActiveIndex == index ? 'selected' : ''">
                  {{item.name}}
                </span>
              </div>
            </div>
          </div>
        </div>
        <!-- 医院列表 -->
        <div class="v-scroll-list hospital-list">
          <div class="v-card clickable list-item"
               v-for="(item, index) in hospitalList" :key="index"
               @click="show(item.hoscode)">
            <div class="">
              <div class="hospital-list-item hos-item" index="0">
                <div class="wrapper">
                  <div class="hospital-title">
                    {{item.hosname}}
                  </div>
                  <div class="bottom-container">
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>
                      {{item.param.hostypeStr}}
                    </div>
                    <div class="icon-wrapper">
                      <span class="iconfont"></span>
                      每天{{item.bookingRule.releaseTime}}放号
                    </div>
                  </div>
                </div>
                <img :src="'data:image/jpeg;base64,'+item.logoData" :alt="item.hosname" class="hospital-img"></div>
            </div>
          </div>
        </div>

        <div class="no-data-wrapper" v-if="hospitalList.length === 0">
          <img  src="//img.114yygh.com/staticres/web/no_data.png" class="no-data-img">
          <span class="no-text">暂无数据</span>
        </div>
      </div>

      <!-- 右侧显示内容 -->
      <div class="right">
        <div class="common-dept">
          <div class="header-wrapper">
            <div class="title"> 常见科室</div>
            <div class="all-wrapper"><span>全部</span>
              <span class="iconfont icon"></span>
            </div>
          </div>
          <div class="content-wrapper">
            <span class="item v-link clickable dark">神经内科 </span>
            <span class="item v-link clickable dark">消化内科 </span>
            <span class="item v-link clickable dark">呼吸内科 </span>
            <span class="item v-link clickable dark">内科 </span>
            <span class="item v-link clickable dark">神经外科 </span>
            <span class="item v-link clickable dark">妇科 </span>
            <span class="item v-link clickable dark"> 产科 </span>
            <span class="item v-link clickable dark">儿科 </span>
          </div>
        </div>
        <div class="space">
          <div class="header-wrapper">
            <div class="title-wrapper">
              <div class="icon-wrapper"><span
                  class="iconfont title-icon"></span>
              </div>
              <span class="title">平台公告</span>
            </div>
            <div class="all-wrapper">
              <span>全部</span>
              <span class="iconfont icon"></span>
            </div>
          </div>
          <div class="content-wrapper">
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark">关于延长北京大学国际医院放假的通知 </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark">北京中医药大学东方医院部分科室医生门诊医 </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark"> 武警总医院号源暂停更新通知 </span>
            </div>
          </div>
        </div>
        <div class="suspend-notice-list space">
          <div class="header-wrapper">
            <div class="title-wrapper">
              <div class="icon-wrapper">
                <span class="iconfont title-icon"></span>
              </div>
              <span class="title">停诊公告</span>
            </div>
            <div class="all-wrapper">
              <span>全部</span>
              <span class="iconfont icon"></span>
            </div>
          </div>
          <div class="content-wrapper">
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark"> 中国人民解放军总医院第六医学中心(原海军总医院)呼吸内科门诊停诊公告 </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark"> 首都医科大学附属北京潞河医院老年医学科门诊停诊公告 </span>
            </div>
            <div class="notice-wrapper">
              <div class="point"></div>
              <span class="notice v-link clickable dark">中日友好医院中西医结合心内科门诊停诊公告 </span>
            </div>
          </div>
        </div>
      </div>

    </div>

  </div>
</template>
<script>
import hospital from "../api/hospital";
import dict from "../api/dict"
export default {
  name: 'index',
  // 服务端渲染，显示医院列表
  asyncData({ params, error }) {
    // api 调用
    return hospital.getPageList(1, 10, null).then(
        response => {
          return {
            hospitalList: response.data.content,
            totalPages: response.data.totalPages
          }
        }
    )
  },
  data() {
    return {
      searchObj: {},
      current: 1,
      limit: 10,

      hosname: '',// 医院名称（查询用）
      hostypeList: [],// 医院等级列表
      districtList: [],// 地区列表

      hostypeActiveIndex: 0,
      districtActiveIndex: 0
    }
  },
  created() {
    this.init();
  },
  mounted() {
    // document.getElementById("search").style.display = 'none';
    // 添加滚动事件，检测滚动到页面底部
    // window.addEventListener('scroll', this.load, true)
  },
  beforeDestroy() {
    // 页面关闭的同时，记得将这个监听器关闭，节省性能
    // window.removeEventListener('scroll', this.load, false)
  },
  methods: {
    // 初始查询
    init() {
      // 1.查询医院等级列表
      dict.findByDictCode("HospitalType").then(
          response => {
            // 清空列表
            this.hostypeList = [];
            // 列表赋值
            for (let i = 0; i < response.data.length; i++) {
              this.hostypeList.push({'name': response.data[i].name, 'value': response.data[i].value})
            }
            // 在列表最前面加入一个 全部 对象
            this.hostypeList.unshift({'name': '全部', 'value': ''});
          }
      )

      // 2.查询所有地区列表
      dict.findByDictCode("Beijing").then(
          response => {
            this.districtList = [];
            // 列表赋值
            for (let i = 0; i < response.data.length; i++) {
              this.districtList.push({'name': response.data[i].name, 'value': response.data[i].value})
            }
            // 在列表最前面加入一个 全部 对象
            this.districtList.unshift({'name': '全部', 'value': ''});
          }
      )
    },

    // 医院等级被选择（根据医院等级做查询）
    hostypeSelect (hostype, index) {
      // 准备数据
      this.current = 1;
      this.hostypeActiveIndex = index;
      // 将查询条件放入查询对象
      this.searchObj = {};
      this.searchObj.hostype = hostype;
      // 查询
      this.getHospitalList();
    },

    // 地区被选择（根据地区做查询）
    districtSelect (districtCode, index) {
      // 准备数据
      this.current = 1;
      this.districtActiveIndex = index;
      // 将查询条件放入查询对象
      this.searchObj.districtCode = districtCode;
      // 查询
      this.getHospitalList();
    },

    // 查询医院列表
    getHospitalList() {
      hospital.getPageList(this.current, this.limit, this.searchObj).then(
          response => {
            this.hospitalList = response.data.content;
            this.totalPages = response.data.totalPages;
          }
      )
    },

    // 在输入框内输入值，弹出下拉框，显示相关内容，并查询医院列表
    querySearchAsync(queryString, cb) {
      // 获取医院列表
      if (queryString == '') {
        // 当查询条件为空的时候，查询所有
        this.searchObj.hosname = '';
        this.getHospitalList();
        return;
      }
      hospital.getHospitalByHosname(queryString).then(
          response => {
            for (let i = 0, len = response.data.length; i <len; i++) {
              // element是通过 value 属性进行数据展示的，所有将 hosname 放入 value 中
              response.data[i].value = response.data[i].hosname
            }

            // 查询医院列表
            this.searchObj.hosname = queryString;
            this.getHospitalList();

            // cb是回调函数，返回筛选出的结果数据到输入框下面的输入列表
            cb(response.data)
          }
      )
    },

    // 跳转到详情页面
    handleSelect(item) {
      window.location.href = '/hospital/' + item.hoscode;
    },

    // 点击医院，进入详请页面
    show(hoscode) {
      window.location.href = '/hospital/' + hoscode
    },

    load(event) {
      // 滚动条高度为430 页面搜索消失，头部搜索显示
      if(event.target.scrollTop > 430) {
        document.getElementById("search").style.display = 'block';
      } else {
        document.getElementById("search").style.display = 'none';
      }

      if(event.target.clientHeight + event.target.scrollTop >= event.target.scrollHeight){
        if(this.current < this.totalPages){      //先判断下一页是否有数据
          this.current = this.current + 1
          this.getHospitalList();              //拉取接口数据
        }
      }
    },

  }
}
</script>
